package com.spark.common.widget

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.os.Build
import android.util.AttributeSet
import android.view.View

class CircleView : View {

    private var mContext: Context? = null
    var mCircleMode = CIRCLE_MODE_NORMAL//圆环模式 默认为普通模式
    var mOuterCircleIsShow = false//外部圆环是否显示  默认不显示
    var mOuterCirclePad = 20//外部圆环距离主圆环边距
    private val extraCirclePaint = Paint()

    var mInnerCircleIsShow = false//内装饰圆是否显示
    var mInnerCirclePad = 20

    var stokenWidth = 20//圆环宽度
    private var angle = 0
    private val paint = Paint()//绘制主圆环画笔
    private val ovalRect = RectF()
    var mRadiusColor = Color.WHITE//圆环颜色

    var issBottomCircleShow = false//底部圆是否显示  默认不显示
    private val bottomPaint = Paint()

    private var indicatorBitmap: Bitmap? = null//圆形指示器Bitmap
    private var bitDistRect: RectF? = null
    private var bitSrcRect: Rect? = null

    var startRotateAngle = MIN_VALUE//圆盘开始角度 默认为最底部0度
    var angleRotateSpan = MAX_VALUE//圆盘可旋转角度范围

    private val tempRect = RectF()

    constructor(context: Context) : super(context) {
        initView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(context, attrs)
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initView(context, attrs)
    }

    private fun initView(mContext: Context, attrs: AttributeSet?) {
        this.mContext = mContext

        paint.color = mRadiusColor//圆圈颜色
        paint.style = Paint.Style.STROKE//设置空心
        paint.strokeWidth = stokenWidth.toFloat()//圆圈宽度设置
        paint.isAntiAlias = true//反锯齿

        bottomPaint.style = Paint.Style.STROKE//设置空心
        bottomPaint.strokeWidth = stokenWidth.toFloat()//圆圈宽度设置

        setCircleMode(mCircleMode)//设置圆环模式

        //装饰圆形 Paint
        extraCirclePaint.color = mRadiusColor//圆为白色
        extraCirclePaint.style = Paint.Style.STROKE//设置空心
        extraCirclePaint.strokeWidth = EXTRA_CIRCLE_WIDTH.toFloat()//圆圈宽度设置
        extraCirclePaint.isAntiAlias = true//反锯齿
    }

    fun setIndicatorBit(bit: Bitmap) {
        indicatorBitmap = bit
        if (indicatorBitmap != null) {
            bitSrcRect = Rect(0, 0, indicatorBitmap!!.width, indicatorBitmap!!.height)
            bitDistRect = RectF(0f, 0f, bitSrcRect!!.width().toFloat(), bitSrcRect!!.height().toFloat())
        }//end if
    }

    //内圆设置
    fun setInnerCircle(isShow: Boolean, pad: Int) {
        this.mInnerCircleIsShow = isShow
        mInnerCirclePad = if (mInnerCircleIsShow) pad else 0
    }

    //外圆设置
    fun setOuterCircle(isShow: Boolean, pad: Int) {
        this.mOuterCircleIsShow = isShow
        mOuterCirclePad = if (mOuterCircleIsShow) pad else 0
    }

    //设置圆圈颜色
    fun setRadiusColor(color: Int) {
        this.mRadiusColor = color
        paint.color = mRadiusColor
        setBottomCircleShow(issBottomCircleShow)
    }

    //设置圆环模式
    fun setCircleMode(mode: Int) {
        mCircleMode = mode
        when (mCircleMode) {
            CIRCLE_MODE_ROUND -> {
                paint.strokeJoin = Paint.Join.ROUND
                paint.strokeCap = Paint.Cap.ROUND //设置圆角
                bottomPaint.strokeJoin = Paint.Join.ROUND
                bottomPaint.strokeCap = Paint.Cap.ROUND //设置圆角
            }
            CIRCLE_MODE_DOT -> {
                val effects = DashPathEffect(floatArrayOf(5f, 5f, 5f, 5f), 10f)
                paint.pathEffect = effects
                bottomPaint.pathEffect = effects
            }
        }//end switch
    }

    //设置底部圆是否显示
    fun setBottomCircleShow(isShow: Boolean) {
        issBottomCircleShow = isShow
        if (issBottomCircleShow) {//修改绘制Paint参数
            bottomPaint.color = paint.color
            bottomPaint.alpha = 0x33
        }//end if
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val w = width
        val h = height

        //圆心点坐标
        val centerX = w shr 1
        val centerY = h shr 1

        canvas.save()
        canvas.rotate((90 + startRotateAngle).toFloat(), centerX.toFloat(), centerY.toFloat())//坐标系旋转

        val outerCircleRadius = (Math.min(w, h) shr 1) - PAD//外圆半径
        if (mOuterCircleIsShow) {//外装饰圆显示
            //canvas.drawCircle(centerX, centerY, outerCircleRadius, extraCirclePaint);
            tempRect.set((centerX - outerCircleRadius).toFloat(), (centerY - outerCircleRadius).toFloat(),
                    (centerX + outerCircleRadius).toFloat(), (centerY + outerCircleRadius).toFloat())
            canvas.drawArc(tempRect, 0f, angleRotateSpan.toFloat(), false, extraCirclePaint)
        }

        //draw Main
        val radius = outerCircleRadius - mOuterCirclePad - (stokenWidth shr 1)
        val left = (w shr 1) - radius
        val top = (h shr 1) - radius
        ovalRect.set(left.toFloat(), top.toFloat(),
                (left + radius + radius).toFloat(), (top + radius + radius).toFloat())
        //绘制底部圆
        if (issBottomCircleShow) {//底部圆显示
            canvas.drawArc(ovalRect, 0f, angleRotateSpan.toFloat(), false, bottomPaint)
        }
        //主圆绘制
        canvas.drawArc(ovalRect, 0f, angle.toFloat(), false, paint)

        //内圆绘制
        if (mInnerCircleIsShow) {//内圆显示
            val innerRadius = radius - (stokenWidth shr 1) - mInnerCirclePad - EXTRA_CIRCLE_WIDTH
            //canvas.drawCircle(centerX, centerY, innerRadius, extraCirclePaint);
            tempRect.set((centerX - innerRadius).toFloat(), (centerY - innerRadius).toFloat(),
                    (centerX + innerRadius).toFloat(), (centerY + innerRadius).toFloat())
            canvas.drawArc(tempRect, 0f, angleRotateSpan.toFloat(), false, extraCirclePaint)
        }

        //绘制指示器
        if (indicatorBitmap != null) {
            val bitWidth = indicatorBitmap!!.width
            val bitHeight = indicatorBitmap!!.height

            //角度制转弧度制
            val radAngle = Math.toRadians(angle.toDouble())
            //圆的参数方程确定坐标点 极坐标
            val x = (radius * Math.cos(radAngle)).toInt() + centerX - (bitDistRect!!.width() / 2).toInt()
            val y = (radius * Math.sin(radAngle)).toInt() + centerY - (bitDistRect!!.height() / 2).toInt()
            bitDistRect!!.set(x.toFloat(), y.toFloat(), x + bitDistRect!!.width(), y + bitDistRect!!.height())
            canvas.drawBitmap(indicatorBitmap!!, bitSrcRect, bitDistRect!!, null)
        }//end if

        canvas.restore()
    }


    fun setStrokenWidth(size: Int) {
        if (size <= 0)
            throw IllegalStateException("size can not be zero or -1")
        stokenWidth = size
        paint.strokeWidth = stokenWidth.toFloat()//圆圈宽度设置
        bottomPaint.strokeWidth = stokenWidth.toFloat()//底部圆圈宽度 应与主圆保持一致
    }

    /**
     * 设置数值
     *
     * @param value
     */
    fun setValue(value: Int) {
        angle = Math.min(value, angleRotateSpan)
        invalidate()//重新设置数值
    }

    companion object {
        val PAD = 30
        val MIN_VALUE = 0
        val MAX_VALUE = 360

        //圆环模式
        private val CIRCLE_MODE_NORMAL = 1//普通模式
        private val CIRCLE_MODE_ROUND = 2//圆角模式(文艺模式)
        private val CIRCLE_MODE_DOT = 3//虚线模式(2B模式)

        private val EXTRA_CIRCLE_WIDTH = 1
    }
}//end class